home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / COMMDLG.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  3KB  |  96 lines

  1. /*
  2.  * COMMDLG.C
  3.  *
  4.  * Routines to interface to the COMMDLG library for File Open and
  5.  * File Save/Save As functions.
  6.  *
  7.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  8.  *
  9.  */
  10.  
  11.  
  12. #include <windows.h>
  13. #include <commdlg.h>
  14. #include "schmoo.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * FSaveOpenDialog
  20.  *
  21.  * Purpose:
  22.  *  Invokes the COMMDLG.DLL GetOpenFileName dialog and retrieves
  23.  *  a filename for saving or opening.
  24.  *
  25.  * Parameters:
  26.  *  hWnd            HWND of the owning application.
  27.  *  hInst           HANDLE of the application instance.
  28.  *  pszExt          LPSTR of the default extension
  29.  *  pszFilter       LPSTR of the filter desciption.
  30.  *  pszFile         LPSTR buffer to receive the entered filename.
  31.  *                  Must be at least CCHPATHMAX long.
  32.  *  fOpen           BOOL indicating if we want file open or save.
  33.  *
  34.  * Return Value:
  35.  *  BOOL            TRUE if the function retrieved a filename,
  36.  *                  FALSE if the user pressed CANCEL.
  37.  */
  38.  
  39. BOOL FAR PASCAL FSaveOpenDialog(HWND hWnd, HANDLE hInst,
  40.                                 LPSTR pszExt, LPSTR pszFilter,
  41.                                 LPSTR pszFile, LPSTR pszCaption,
  42.                                 BOOL fOpen)
  43.     {
  44.     OPENFILENAME    ofn;
  45.     char            szTitle[CCHFILENAMEMAX];
  46.     char            szFilter[80];
  47.     WORD            cch1;
  48.     WORD            cch2;
  49.  
  50.  
  51.     ofn.lStructSize      =sizeof(OPENFILENAME);
  52.     ofn.hwndOwner        =hWnd;
  53.     ofn.hInstance        =hInst;
  54.  
  55.     ofn.lpstrFilter      =szFilter;
  56.     ofn.lpstrCustomFilter=NULL;
  57.     ofn.nMaxCustFilter   =0L;
  58.     ofn.nFilterIndex     =1L;                //We only have 1 extension.
  59.  
  60.     ofn.lpstrFile        =pszFile;
  61.     ofn.nMaxFile         =CCHPATHMAX;
  62.     ofn.lpstrFileTitle   =(LPSTR)szTitle;
  63.     ofn.nMaxFileTitle    =CCHFILENAMEMAX;
  64.  
  65.     ofn.lpstrInitialDir  =NULL;
  66.     ofn.lpstrTitle       =pszCaption;
  67.  
  68.     ofn.Flags            =OFN_HIDEREADONLY;
  69.     ofn.nFileOffset      =0;
  70.     ofn.nFileExtension   =0;
  71.     ofn.lpstrDefExt      =pszExt;
  72.     ofn.lCustData        =NULL;
  73.     ofn.lpfnHook         =NULL;
  74.     ofn.lpTemplateName   =NULL;
  75.  
  76.  
  77.     //Modify the flags as appropriate.
  78.     if (fOpen)
  79.         ofn.Flags        |= OFN_FILEMUSTEXIST;
  80.     else
  81.         ofn.Flags        |= OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  82.  
  83.  
  84.     //Build a filter like "pszFilter\0*.pszExt\0\0"
  85.     lstrcpy(szFilter, pszFilter);
  86.     cch1=1+lstrlen(szFilter);
  87.  
  88.     cch2=wsprintf(pszFile, "*.%s", pszExt);  //Initial edit control contents.
  89.     lstrcpy(szFilter+cch1, pszFile);         //Append to filter.
  90.  
  91.     //Add the second null-terminator.
  92.     *(szFilter+cch1+cch2+1)=0;
  93.  
  94.     return GetOpenFileName(&ofn);
  95.     }
  96.